package gui;


import flexibility.downloadManager;
import flexibility.showMessage;
import guiCode.studentScreenCode;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import javax.swing.JButton;

import basic.ClientConsole;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.ScrollPane;
import java.awt.Button;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.JRadioButton;

import com.sun.rowset.CachedRowSetImpl;

import secretary.defineSemester;
import student.assignmentInfo;
import student.fillAssignments;
import student.fillComboBox;
import student.fillCourses;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JComboBox;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

//It's a student's main window when he make log in
public class studentScreen  {
	
	
	private static final long serialVersionUID = 1L;
	private showMessage msg=new showMessage();
	private static JFrame studentScreenWindow;
	private ClientConsole myConnection;
	public defineSemester semester=new defineSemester();
	static studentScreenCode student_scr_code;
	public JTable table;
	public JTable tableUp;
	public static JComboBox comboBox;
	private CachedRowSetImpl crs;
	private CachedRowSetImpl rs;
	public fillCourses FC;
	public fillAssignments FA;
	public ArrayList<assignmentInfo> infoAssignmentArr;
	
	
	public studentScreen(ClientConsole connection, Integer id) {
		myConnection=connection;
		student_scr_code= new studentScreenCode();
		setID(id);  // set user's Id
		setConnection(connection);
		SetCurrentSemester(semester.identify(0, myConnection, crs));
		System.out.println(semester.identify(0, myConnection, crs));
		createStudentWindow();
	}
	public void createStudentWindow(){
		studentScreenWindow = new JFrame("Academic Information System - Student");
		studentScreenWindow.getContentPane().setBackground(new Color(240, 255, 255));
		studentScreenWindow.setSize(811, 650);
		studentScreenWindow.setLocation(new Point(250,50));
		studentScreenWindow.setResizable(false);
		SpringLayout springLayout = new SpringLayout();
		studentScreenWindow.getContentPane().setLayout(springLayout);
		
		///////////////////////////////////
		// "upload assignment" button		
		//////////////////////////////////////
		JButton btnUploadAssignment = new JButton("Upload Assignment");
		springLayout.putConstraint(SpringLayout.WEST, btnUploadAssignment, 21, SpringLayout.WEST, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, btnUploadAssignment, -237, SpringLayout.SOUTH, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnUploadAssignment, -654, SpringLayout.EAST, studentScreenWindow.getContentPane());
		btnUploadAssignment.setForeground(Color.BLACK);
		btnUploadAssignment.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 11));
		//"upload assignment" button listener 
		btnUploadAssignment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				student_scr_code.runUploadManager();
			}
		  }); 
		
		studentScreenWindow.getContentPane().add(btnUploadAssignment);
		
		///////////////////////////////////
		//Close  studentScreen window  if the user clicks on "X"
		////////////////////////////////////
		studentScreenWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		studentScreenWindow.addWindowListener(new WindowAdapter() {
			    public void windowClosing(WindowEvent e) {
					student_scr_code.closeStudentWindow();
			    }
		});
		
		/////////////////////////////
		//Download button
		/////////////////////////////
		JButton btnDownload = new JButton("Download");
		springLayout.putConstraint(SpringLayout.NORTH, btnDownload, 24, SpringLayout.SOUTH, btnUploadAssignment);
		springLayout.putConstraint(SpringLayout.WEST, btnDownload, 21, SpringLayout.WEST, studentScreenWindow.getContentPane());
		btnDownload.setForeground(Color.BLACK);
		btnDownload.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 11));
		btnDownload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// The constructor of downloadManager gets the connection to server and the fileID you want to download 
				downloadManager downloadMngr = new downloadManager(myConnection,70);
				downloadMngr.downloadFile();
			}
		});
		studentScreenWindow.getContentPane().add(btnDownload);
		
		////////////////////////////
		//Define Down Table
		//////////////////////////////
		table = new JTable();
		table.setSize(new Dimension(2, 0));
		table.setCellSelectionEnabled(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
			},
			new String[] {
				"New column", "New column", "New column", "New column", "New column"
			}
		));
		springLayout.putConstraint(SpringLayout.NORTH, table, 56, SpringLayout.NORTH, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, table, -251, SpringLayout.EAST, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, table, 152, SpringLayout.NORTH, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, table, -250, SpringLayout.EAST, studentScreenWindow.getContentPane());
			
		JScrollPane scrollPane = new JScrollPane(table);
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -64, SpringLayout.SOUTH, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnDownload, -35, SpringLayout.WEST, scrollPane);
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.EAST, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 35, SpringLayout.EAST, btnUploadAssignment);
		studentScreenWindow.getContentPane().add(scrollPane);
		
		
		///////////////////////
		//Student's Info Labels
		////////////////////////
		 String[] name=new String[2];
		 name=student_scr_code.getStudentName();
				 
		JLabel lblFirstName = new JLabel(name[0]+" "+name[1]);
		lblFirstName.setFont(new Font("Century", Font.BOLD, 13));
		studentScreenWindow.getContentPane().add(lblFirstName);
		
		JLabel lblStudentID = new JLabel(student_scr_code.getMyID().toString());
		springLayout.putConstraint(SpringLayout.NORTH, lblStudentID, 12, SpringLayout.SOUTH, lblFirstName);
		springLayout.putConstraint(SpringLayout.WEST, lblStudentID, 0, SpringLayout.WEST, lblFirstName);
		springLayout.putConstraint(SpringLayout.EAST, lblStudentID, -597, SpringLayout.EAST, studentScreenWindow.getContentPane());
		lblStudentID.setFont(new Font("Century", Font.BOLD, 12));
		studentScreenWindow.getContentPane().add(lblStudentID);
		
		JLabel lblClass = new JLabel(student_scr_code.getClassName());
		springLayout.putConstraint(SpringLayout.SOUTH, lblStudentID, -14, SpringLayout.NORTH, lblClass);
		springLayout.putConstraint(SpringLayout.WEST, lblClass, 0, SpringLayout.WEST, lblFirstName);
		lblClass.setFont(new Font("Century", Font.BOLD, 12));
		studentScreenWindow.getContentPane().add(lblClass);
		
		JLabel lblInfo = new JLabel("<html>Student's Name:<br><br>Student's ID:<br><br>Student's Class:</html>");
		springLayout.putConstraint(SpringLayout.SOUTH, lblClass, 0, SpringLayout.SOUTH, lblInfo);
		springLayout.putConstraint(SpringLayout.NORTH, lblFirstName, -2, SpringLayout.NORTH, lblInfo);
		springLayout.putConstraint(SpringLayout.WEST, lblFirstName, 5, SpringLayout.EAST, lblInfo);
		springLayout.putConstraint(SpringLayout.NORTH, lblInfo, 70, SpringLayout.NORTH, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblInfo, 21, SpringLayout.WEST, studentScreenWindow.getContentPane());
		studentScreenWindow.getContentPane().add(lblInfo);
		
		/////////////////////////
		//Logout Button
		/////////////////////////
		JButton btnLogOut = new JButton("Logout");
		springLayout.putConstraint(SpringLayout.NORTH, btnLogOut, 10, SpringLayout.NORTH, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnLogOut, 0, SpringLayout.EAST, scrollPane);
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				student_scr_code.closeStudentWindow();
			}
		});
		studentScreenWindow.getContentPane().add(btnLogOut);
		
		////////////////////////////
		//Define Up Table
		//////////////////////////
		tableUp = new JTable();
		springLayout.putConstraint(SpringLayout.WEST, btnLogOut, 491, SpringLayout.EAST, tableUp);
		tableUp.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//tableUp.getValueAt(tableUp.rowAtPoint(e.getPoint()), tableUp.columnAtPoint(e.getPoint()));
				String courseIDString=(tableUp.getValueAt(tableUp.rowAtPoint(e.getPoint()), 0).toString());
				System.out.println("selected courseID as String: "+courseIDString);
				if (courseIDString==""){
					
				}
				else{
					FA = new fillAssignments(myConnection,student_scr_code.getMyID());
					int courseID=FA.getCourseIdAsInt(courseIDString);
					FA.setCourseId(courseID);
					FA.createViews(FC);
					crs=FA.getFullTable();
					infoAssignmentArr=FA.fillAssignmentList(crs);
					crs=FA.getInformationTable();
					FA.update(studentScreen.this, crs);
					FA.deleteAllViews();
					
				}
			}
		});
		springLayout.putConstraint(SpringLayout.NORTH, tableUp, 0, SpringLayout.NORTH, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, tableUp, 724, SpringLayout.WEST, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, tableUp, -234, SpringLayout.NORTH, btnUploadAssignment);
		springLayout.putConstraint(SpringLayout.EAST, tableUp, -109, SpringLayout.EAST, studentScreenWindow.getContentPane());
		tableUp.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		tableUp.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
			},
			new String[] {
				"New column", "New column", "New column"
			}
		));
		tableUp.setCellSelectionEnabled(true);
		studentScreenWindow.getContentPane().add(tableUp);
		
		JScrollPane scrollPaneUP = new JScrollPane(tableUp);
		springLayout.putConstraint(SpringLayout.EAST, scrollPaneUP, -10, SpringLayout.EAST, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 25, SpringLayout.SOUTH, scrollPaneUP);
		springLayout.putConstraint(SpringLayout.NORTH, scrollPaneUP, -9, SpringLayout.NORTH, lblClass);
		scrollPaneUP.setPreferredSize(new Dimension(200, 200));
		studentScreenWindow.getContentPane().add(scrollPaneUP);
		
		//////////////////
		///Define ComboBox
		///////////////////
		comboBox = new JComboBox();
		springLayout.putConstraint(SpringLayout.WEST, scrollPaneUP, 33, SpringLayout.EAST, comboBox);
		springLayout.putConstraint(SpringLayout.EAST, comboBox, -300, SpringLayout.EAST, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, comboBox, 0, SpringLayout.NORTH, scrollPaneUP);
		//Fill the ComboBox with all stdudent's semesters
		fillComboBox fillCombo=new fillComboBox(myConnection,student_scr_code.getMyID());
		comboBox.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent e) {
				System.out.println("itemStateChanged() to ");
				}
		});
		studentScreenWindow.getContentPane().add(comboBox);
		
		////////////////////
		//Show courses button
		/////////////////////
		JButton btnShowCourses = new JButton("Show Courses");
		springLayout.putConstraint(SpringLayout.NORTH, btnShowCourses, 18, SpringLayout.SOUTH, comboBox);
		springLayout.putConstraint(SpringLayout.SOUTH, btnShowCourses, -164, SpringLayout.NORTH, scrollPane);
		springLayout.putConstraint(SpringLayout.WEST, comboBox, 0, SpringLayout.WEST, btnShowCourses);
		springLayout.putConstraint(SpringLayout.EAST, btnShowCourses, -300, SpringLayout.EAST, studentScreenWindow.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, btnShowCourses, 381, SpringLayout.WEST, studentScreenWindow.getContentPane());
		studentScreenWindow.getContentPane().add(btnShowCourses);
		
		btnShowCourses.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String sem = (String) comboBox.getSelectedItem();
				System.out.println("Selected Item: "+sem);
				FC = new fillCourses(myConnection,student_scr_code.getMyID());
				FC.setSemesterNameandYear(sem);
				FC.setSemesterID();
				rs=FC.getCourses();
				FC.update(studentScreen.this, rs);
				//Clear the Down Table
				crs=FA.clearTable();
				FA.update(studentScreen.this, crs);
			}
		});
		
		////////////////////
		//Select Semester label
		//////////////////////
		Label selectSemesterlbl = new Label("Select Semester :");
		springLayout.putConstraint(SpringLayout.WEST, selectSemesterlbl, 0, SpringLayout.WEST, comboBox);
		springLayout.putConstraint(SpringLayout.SOUTH, selectSemesterlbl, 0, SpringLayout.SOUTH, lblStudentID);
		springLayout.putConstraint(SpringLayout.EAST, selectSemesterlbl, -321, SpringLayout.EAST, studentScreenWindow.getContentPane());
		selectSemesterlbl.setFont(new Font("Times New Roman", Font.BOLD, 12));
		studentScreenWindow.getContentPane().add(selectSemesterlbl);
		
		
		
		studentScreenWindow.setVisible(true);
		}
		
		
	
	public void setID(int id) {
		student_scr_code.setMyID(id);// sets the id of the user

	}
	
	public void setConnection(ClientConsole conn) {
		student_scr_code.setConnection(conn);// sets the connection to server of the
								// secretary
	}
	
	public void SetCurrentSemester(int id){
		student_scr_code.setCurrentSemester(id);
	}
}
